package com.vbez.sommelier.rawinfo;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class LJPageParser {

	// There could be several posting in the same day.
	// So we could have one web page with several postings.
	// Method "parse" recursively parses all text for the day.
	// "startFrom" indicates from what position parsing should be started.
	public List<RawItem> parse(StringBuilder sb, int startFrom) {
		List<RawItem> items = new ArrayList<RawItem>();

		RawItem item = new RawItem();

		// get URL of page for next day
		// Please note that this link presence for all day but not for each
		// posting in the same day.
		String startText = "Previous Day</a> | <a href=\"";
		String endText = "\">Next Day";
		int startIdx = sb.indexOf(startText, 0);
		int endIdx = -1;
		if (startIdx != -1) {
			endIdx = sb.indexOf(endText, startIdx + startText.length());
			item.nextDayLink = sb.substring(startIdx + startText.length(),
					endIdx);
		}
		// Following parts are unique for each posting.
		// If there will be several postings for the same day
		// all these information will be different for each post.

		String startItemText = "<div class=\"subcontent\">";
		String endItemText = "<div class=\"clear\">&nbsp;</div>";
		int startItemIdx = sb.indexOf(startItemText, startFrom);
		if (startItemIdx == -1) {
			return items;
		}
		startFrom = startItemIdx + startItemText.length();
		int endItemIdx = sb.indexOf(endItemText, startFrom);

		String itemText = sb.substring(startFrom,
				endItemIdx + endItemText.length());
		/*
		 * startText = "<div class=\"datesubject\">"; startFrom =
		 * sb.indexOf(startText, startFrom);
		 */

		// get date/time
		startText = "<div class=\"date\">";
		endText = "</div>";
		startIdx = itemText.indexOf(startText);
		endIdx = itemText.indexOf(endText, startIdx + startText.length());
		item.dateTime = itemText.substring(startIdx + startText.length(),
				endIdx);
		// startFrom = endIdx + endText.length();

		startText = "<div class=\"subject\"><a class=\"lj_invisible\">(no subject)</a>";
		startIdx = itemText.indexOf(startText);
		if (startIdx == -1) {
			// get URL of current posting
			startText = "<a href=\"";
			endText = "\"";
			startIdx = itemText.indexOf(startText);
			endIdx = itemText.indexOf(endText, startIdx + startText.length());
			item.link = itemText.substring(startIdx + startText.length(),
					endIdx);
			// startFrom = endIdx + endText.length();

			startText = "class=\"subj-link\" >";
			endText = "</a>";
			startIdx = itemText.indexOf(startText);
			endIdx = itemText.indexOf(endText, startIdx + startText.length());
			item.subject = itemText.substring(startIdx + startText.length(),
					endIdx);
			// startFrom = endIdx + endText.length();
		}
		startText = "<div class=\"entry_text\">";
		endText = "<div class=\'ljtags\'>Tags: ";
		startIdx = itemText.indexOf(startText);
		endIdx = itemText.indexOf(endText, startIdx + startText.length());
		if (endIdx == -1) {
			endText = "<div class=\"clear\">&nbsp;</div>";
			endIdx = itemText.indexOf(endText, startIdx + startText.length());
		}
		item.description = itemText.substring(startIdx + startText.length(),
				endIdx);
		// startFrom = endIdx + endText.length();

		int entryTextStartIdx = startIdx + startText.length();
		// int entryTextEndIdx = endIdx + endText.length();

		startText = "<img src=\"";
		endText = "\"";
		startIdx = itemText.indexOf(startText, entryTextStartIdx);
		if (startIdx != -1) {
			endIdx = itemText.indexOf(endText, startIdx + startText.length());
			item.pictureURL = itemText.substring(startIdx + startText.length(),
					endIdx);
		}

		startText = "<div class=\'ljtags\'>Tags: ";
		endText = "<div class=\"clear\">&nbsp;</div>";
		startIdx = itemText.indexOf(startText, entryTextStartIdx);
		if (startIdx != -1) {
			endIdx = itemText.indexOf(endText, startIdx + startText.length());
			String tagsText = itemText.substring(startIdx, endIdx);

			startText = ">";
			endText = "<";
			StringTokenizer st = new StringTokenizer(tagsText, ",");
			while (st.hasMoreTokens()) {
				String tagElement = st.nextToken();
				startIdx = tagElement.indexOf(startText);
				endIdx = tagElement.indexOf(endText,
						startIdx + startText.length());
				String tag = tagElement.substring(
						startIdx + startText.length(), endIdx);
				item.tags.add(tag);
			}
		}

		items.add(item);

		items.addAll(parse(sb, startFrom));

		return items;
	}
}